/**
* Copyright (c) 2004-2005 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

/**
 *
 * EXECUTE:
 * java -cp jenoware.jar CompareCSV file1.csv file2.csv
 *
 * The program opens two CSV files and prints the difference between markers.
 *
 */

import java.util.HashMap;
// import jenoware packages
import it.ciano.jenoware.*;

public class CompareCSV {
    public CompareCSV() {}

    public static void main ( String[] args )
    throws java.io.IOException {
        // genotyped
        HashMap genotyped = new HashMap();

        // errors
        HashMap errors = new HashMap();
        HashMap perrors = new HashMap();

        // the Pedigree object
        Pedigree ped1 = new Pedigree();
        Pedigree ped2 = new Pedigree();

        // add all the CSV files to the Pedigree
        toPedigree.importCSV ( ped1, args[ 0 ], ";" );
        toPedigree.importCSV ( ped2, args[ 1 ], ";" );

        // list of id contained into the two csv file
        Sbj[] id1 = ped1.getListSbj();

        // markers list
        String[] markers1 = ped1.getMarkersList();
        String[] markers2 = ped2.getMarkersList();

        // phenotype list
        String[] pheno1 = ped1.getPhenotypesList();
        String[] pheno2 = ped2.getPhenotypesList();

        // if exist phenotypes compare the two pedigree
        if ( pheno1 != null && pheno2 != null ) {
            System.out.println ( "Comparing phenotypes in CSV files..." );
            for ( int i = 0; i < pheno1.length; i++ ) {
                perrors.put ( pheno1[ i ], new Integer ( 0 ) );
                System.out.println ( "\nphenotype: " + pheno1[ i ] + "\n" );
                for ( int j = 0; j < id1.length; j++ ) {
                    Phenotype p1 = id1[ j ].getPhenotype ( pheno1[ i ] );
                    Sbj s2 = ped2.getSBJ ( id1[ j ].getID() );
                    Phenotype p2 = new Phenotype ( pheno1[ i ], "" );
                    if ( s2 != null )
                        p2 = s2.getPhenotype ( pheno1[ i ] );
                    if ( p1.equals ( p2 ) ) {
                        System.out.println ( "   id: " + id1[ j ] + " file1: " + p1 + " == 2: " + p2 );
                    } else {
                        System.out.println ( "   id: " + id1[ j ] + " file1: " + p1 + " || 2: " + p2 );
                        errors.put ( pheno1[ i ],
                        new Integer ( ( ( Integer ) perrors.get ( pheno1[ i ] ) ).intValue() + 1 ) );
                    }
                }

                // print summary results
                int _perrors = ( ( Integer ) perrors.get ( pheno1[ i ] ) ).intValue();
                System.out.println ( "\n  Result Summaries " );
                System.out.println ( "PhenoErrors: " + _perrors );
            }
        }

        // if exist markers compare the two pedigree
        if ( markers1 != null && markers2 != null ) {
            System.out.println ( "Comparing markers in CSV files..." );
            for ( int i = 0; i < markers1.length; i++ ) {
                errors.put ( markers1[ i ], new Integer ( 0 ) );
                genotyped.put ( markers1[ i ], new Integer ( 0 ) );
                System.out.println ( "\nmarker: " + markers1[ i ] + "\n" );
                for ( int j = 0; j < id1.length; j++ ) {
                    Genotype g1 = id1[ j ].getGenotype ( markers1[ i ] );
                    Sbj s2 = ped2.getSBJ ( id1[ j ].getID() );
                    Genotype g2 = new Genotype ( markers1[ i ] );
                    if ( s2 != null )
                        g2 = s2.getGenotype ( markers1[ i ] );
                    if ( !g1.equals ( g2 ) ) {
                        System.out.println ( "   id: " + id1[ j ] + " file1: " + g1 + " || 2: " + g2 );
                        errors.put ( markers1[ i ],
                        new Integer ( ( ( Integer ) errors.get ( markers1[ i ] ) ).intValue() + 1 ) );
                    }
                    if ( g1.isValid() )
                        genotyped.put ( markers1[ i ],
                        new Integer ( ( ( Integer ) genotyped.get ( markers1[ i ] ) ).intValue() + 1 ) );
                }

                // print summary results
                int _genotyped = ( ( Integer ) genotyped.get ( markers1[ i ] ) ).intValue();
                int _errors = ( ( Integer ) errors.get ( markers1[ i ] ) ).intValue();
                System.out.println ( "\n  Result Summaries " );
                System.out.println ( "  Genotyped: " + _genotyped );
                System.out.println ( "     Errors: " + _errors );
                if ( _genotyped > 0 ) {
                    float _binning_error_rate = ( float ) _errors / _genotyped;
                    System.out.println ( "Errors rate: " + _binning_error_rate );
                }

            }
        }
    } // end main

} //end class

